$(function () {
	var system_type_box = $('.system_type_box');
	init_system(system_type_box);

	system_type_box.on('change', '.system_type', function () {
		set_system_type(system_type_box, $(this).val());
		set_item_type(system_type_box, 0);
		set_detail_type(system_type_box, 0);
	});
	system_type_box.on('change', '.item_type', function () {
		set_item_type(system_type_box, $(this).val());
		set_detail_type(system_type_box, 0);
	});
});

function init_system(system_type_box) {
	//todo:整理数组
	system_type_json = [];
	item_type_json = [];
	detail_type_json = [];
	for (var i in system_array) {
		if (system_array[i].fid==0) {
			system_type_json.push(system_array[i]);
		}
	}

	for (var i in system_array) {
		for (var j in system_type_json) {
			if (system_array[i].fid == system_type_json[j].id) {
				item_type_json.push(system_array[i]);
			}
		}
	}

	for (var i in system_array) {
		for (var j in item_type_json) {
			if (item_type_json[j].id == system_array[i].fid) {
				detail_type_json.push(system_array[i]);
			}
		}
	}

	var system_type = system_type_box.find('.system_type').attr('_data_id');
	var item_type = system_type_box.find('.item_type').attr('_data_id');
	var detail_type = system_type_box.find('.detail_type').attr('_data_id');
	set_system_type(system_type_box, system_type);
	set_item_type(system_type_box, item_type);
	set_detail_type(system_type_box, detail_type);
}

function set_system_type(system_type_box, system_type_id) {
	var system_type = system_type_box.find('.system_type');
	var system_type_html = '<option value="">请选择</option>';
	for (var i in system_type_json) {
		var selected = '';
		if (system_type_id && system_type_id==system_type_json[i].id) {
			selected = 'selected';
		}
		system_type_html += '<option value="' + system_type_json[i].id + '" ' + selected + '>' + system_type_json[i].name + '</option>';
	}
	system_type.html(system_type_html);
}

function set_item_type(system_type_box, item_type_id) {
	var item_type = system_type_box.find('.item_type');
	var system_type = system_type_box.find('.system_type');
	var item_type_html = '<option value="">请选择</option>';
	var system_type_id = system_type.val();
	if (system_type_id) {
		for (var i in item_type_json) {
			if (system_type_id==item_type_json[i].fid) {
				var selected = '';
				if (item_type_id && item_type_id==item_type_json[i].id) {
					selected = 'selected';
				}
				item_type_html += '<option value="' + item_type_json[i].id + '" ' + selected + '>' + item_type_json[i].name + '</option>';
			}
		}
	}
	item_type.html(item_type_html);
}

function set_detail_type(system_type_box, detail_type_id) {
	var item_type = system_type_box.find('.item_type');
	var detail_type = system_type_box.find('.detail_type');
	var detail_type_html = '<option value="">请选择</option>';
	var item_type_id = item_type.val();
	if (item_type_id) {
		for (var i in detail_type_json) {
			if (item_type_id==detail_type_json[i].fid) {
				var selected = '';
				if (detail_type_id && detail_type_id==detail_type_json[i].id) {
					selected = 'selected';
				}
				detail_type_html += '<option value="' + detail_type_json[i].id + '" ' + selected + '>' + detail_type_json[i].name + '</option>';
			}
		}
	}
	detail_type.html(detail_type_html);
}